package bTree.LeetCode;

public class 目标和 {
    public int findTargetSumWays(int[] nums, int target) {
        int sum = 0;
        for (int num : nums) {
            sum += num;
        }
        //通过数学公式推出
        int pack = (sum + target) / 2;
        if ((sum + target) % 2 == 1||Math.abs(target)>sum) {
            return 0;
        }
        int[] dp = new int[pack + 1];
        dp[0] = 1;
        for (int i = 0; i < nums.length; i++) {
            for (int j = pack; j >= nums[i]; j--) {
                //装满背包有多少种
                dp[j] += dp[j - nums[i]];
            }
        }
        return dp[pack];

    }
}